﻿<#
Written by Ryan Ephgrave for ConfigMgr 2012 PS Right Click Tools
http://myitforum.com/myitforumwp/author/ryan2065/
#>

$CompName = $args[0]
$Popup = new-object -comobject wscript.shell

function GenerateForm {

#region Import the Assemblies
[reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
#endregion

#region Generated Form Objects
$DevForm = New-Object System.Windows.Forms.Form
$HideChkBox = New-Object System.Windows.Forms.CheckBox
$RunBtn = New-Object System.Windows.Forms.Button
$BrowseBtn = New-Object System.Windows.Forms.Button
$ScriptToRunLbl = New-Object System.Windows.Forms.Label
$RunBox = New-Object System.Windows.Forms.TextBox
$label1 = New-Object System.Windows.Forms.Label
$ScriptBox = New-Object System.Windows.Forms.ListBox
$openFileDialog1 = New-Object System.Windows.Forms.OpenFileDialog
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
#endregion Generated Form Objects

$BrowseBtn_OnClick= 
{
	$openFileDialog1.Filter = "Scripts (bat,cmd,vbs,ps1)|*.bat;*.cmd;*.vbs;*.ps1"
	$openFileDialog1.ShowDialog()
	$RunBox.Text = $openFileDialog1.Filename
}

$RunBtn_OnClick= 
{
	$RunFile = $RunBox.Text
	if ($RunFile.EndsWith(".cmd") -or $RunFile.EndsWith(".bat")) {
		if ($HideChkBox.Checked) { & Cmd /c $RunFile "$CompName" }
		else {& Start $RunFile "$CompName"}
	}
	elseif ($RunFile.EndsWith(".vbs")) {
		if ($HideChkBox.Checked) {& cscript.exe $RunFile "$CompName"}
		else {& wscript.exe $RunFile "$CompName"}
	}
	elseif ($RunFile.EndsWith(".ps1")) {
		if ($HideChkBox.Checked) {& wscript.exe "C:\Program Files\SCCMConsoleExtensions\SilentOpenPS.vbs" $RunFile "$CompName"}
		else {& wscript.exe "C:\Program Files\SCCMConsoleExtensions\OpenPS.vbs" $RunFile "$CompName"}
	}
	else {
		$msg = "Please select a file. It should end in`n.cmd`n.bat`n.vbs`n.ps1"
		$Popup.Popup("$msg",0,"Error",0)
	}
}

$ScriptBox_OnClick=
{
	$RunBox.Text = "C:\Program Files\SCCMConsoleExtensions\Custom Scripts\" + $ScriptBox.SelectedItem
}

$OnLoadForm_StateCorrection=
{
	$DevForm.WindowState = $InitialFormWindowState
	Get-ChildItem "C:\Program Files\SCCMConsoleExtensions\Custom Scripts\*" -Force -Include ("*.bat","*.cmd","*.vbs","*.ps1") |  ForEach-Object {$ScriptBox.Items.Add($_.Name)}
	Get-ChildItem "C:\Program Files\SCCMConsoleExtensions\Custom Scripts\Devices\*" -Force -Include ("*.bat","*.cmd","*.vbs","*.ps1") |  ForEach-Object {
		$FolderName = "Devices\" + $_.Name
		$ScriptBox.Items.Add($FolderName)
	}
}

#----------------------------------------------
#region Generated Form Code
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 468
$System_Drawing_Size.Width = 464
$DevForm.ClientSize = $System_Drawing_Size
$DevForm.DataBindings.DefaultDataSourceUpdateMode = 0
$DevForm.Name = "DevForm"
$DevForm.Text = "$CompName - Custom Script"

$HideChkBox.Anchor = 10

$HideChkBox.DataBindings.DefaultDataSourceUpdateMode = 0

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 301
$System_Drawing_Point.Y = 379
$HideChkBox.Location = $System_Drawing_Point
$HideChkBox.Name = "HideChkBox"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 24
$System_Drawing_Size.Width = 151
$HideChkBox.Size = $System_Drawing_Size
$HideChkBox.TabIndex = 6
$HideChkBox.Text = "Hide Command Window"
$HideChkBox.UseVisualStyleBackColor = $True

$DevForm.Controls.Add($HideChkBox)

$RunBtn.Anchor = 14

$RunBtn.DataBindings.DefaultDataSourceUpdateMode = 0

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 187
$System_Drawing_Point.Y = 435
$RunBtn.Location = $System_Drawing_Point
$RunBtn.Name = "RunBtn"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 75
$RunBtn.Size = $System_Drawing_Size
$RunBtn.TabIndex = 5
$RunBtn.Text = "Run"
$RunBtn.UseVisualStyleBackColor = $True
$RunBtn.add_Click($RunBtn_OnClick)

$DevForm.Controls.Add($RunBtn)

$BrowseBtn.Anchor = 10

$BrowseBtn.DataBindings.DefaultDataSourceUpdateMode = 0

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 377
$System_Drawing_Point.Y = 409
$BrowseBtn.Location = $System_Drawing_Point
$BrowseBtn.Name = "BrowseBtn"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 75
$BrowseBtn.Size = $System_Drawing_Size
$BrowseBtn.TabIndex = 4
$BrowseBtn.Text = "Browse"
$BrowseBtn.UseVisualStyleBackColor = $True
$BrowseBtn.add_Click($BrowseBtn_OnClick)

$DevForm.Controls.Add($BrowseBtn)

$ScriptToRunLbl.Anchor = 6
$ScriptToRunLbl.DataBindings.DefaultDataSourceUpdateMode = 0

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 12
$System_Drawing_Point.Y = 383
$ScriptToRunLbl.Location = $System_Drawing_Point
$ScriptToRunLbl.Name = "ScriptToRunLbl"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 118
$ScriptToRunLbl.Size = $System_Drawing_Size
$ScriptToRunLbl.TabIndex = 3
$ScriptToRunLbl.Text = "Script To Run:"
$ScriptToRunLbl.TextAlign = 16

$DevForm.Controls.Add($ScriptToRunLbl)

$RunBox.Anchor = 14
$RunBox.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 12
$System_Drawing_Point.Y = 409
$RunBox.Location = $System_Drawing_Point
$RunBox.Name = "RunBox"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 20
$System_Drawing_Size.Width = 359
$RunBox.Size = $System_Drawing_Size
$RunBox.TabIndex = 2

$DevForm.Controls.Add($RunBox)

$label1.DataBindings.DefaultDataSourceUpdateMode = 0

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 12
$System_Drawing_Point.Y = 5
$label1.Location = $System_Drawing_Point
$label1.Name = "label1"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 21
$System_Drawing_Size.Width = 440
$label1.Size = $System_Drawing_Size
$label1.TabIndex = 1
$label1.Text = "Scripts stored in C:\Program Files\SCCMConsoleExtensions\Custom Scripts"

$DevForm.Controls.Add($label1)

$ScriptBox.Anchor = 15
$ScriptBox.DataBindings.DefaultDataSourceUpdateMode = 0
$ScriptBox.FormattingEnabled = $True
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 12
$System_Drawing_Point.Y = 29
$ScriptBox.Location = $System_Drawing_Point
$ScriptBox.Name = "ScriptBox"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 342
$System_Drawing_Size.Width = 440
$ScriptBox.Size = $System_Drawing_Size
$ScriptBox.TabIndex = 0
$ScriptBox.add_Click($ScriptBox_OnClick)

$DevForm.Controls.Add($ScriptBox)

$openFileDialog1.FileName = ""
$openFileDialog1.ShowHelp = $True

#endregion Generated Form Code

#Save the initial state of the form
$InitialFormWindowState = $DevForm.WindowState
#Init the OnLoad event to correct the initial state of the form
$DevForm.add_Load($OnLoadForm_StateCorrection)
#Show the Form
$DevForm.ShowDialog()| Out-Null

} #End Function

#Call the Function
GenerateForm
